package com.enqualcomm.kids.mvp.terminalbill;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.bean.TerminalBillMessage;
import com.enqualcomm.kids.view.dialog.DeleteBillMessageCallBack;
import com.enqualcomm.kids.view.dialog.DeleteBillMessageDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import common.utils.MyLogger;

/**
 * Created by Administrator on 2015/11/2.
 */
public class CheckBillMessageAdapter extends BaseAdapter {

    private List<TerminalBillMessage> messageList;
    private Context context;
    private DeleteBillMessageCallBack callBack;

    public CheckBillMessageAdapter(Context context, DeleteBillMessageCallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        messageList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int i) {
        return messageList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.checkbill_listview_item, null);
            viewHolder.mTimeLine = (LinearLayout) view.findViewById(R.id.line);
            viewHolder.mTime = (TextView) view.findViewById(R.id.time);
            viewHolder.mContent = (TextView) view.findViewById(R.id.content);
            viewHolder.mDatetime_tv = (TextView) view.findViewById(R.id.current_time);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        /**设置时间分隔线时间*/
        TerminalBillMessage message = messageList.get(i);
        viewHolder.mTime.setText(new SimpleDateFormat("MM-dd HH:mm",Locale.getDefault()).format(new Date(message.time)));
        viewHolder.mContent.setText(message.message);

        /**设置消息TextView的长按点击时间，获取ListView中的焦点弹出对话框*/
        viewHolder.mContent.setFocusable(true);
        viewHolder.mContent.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new DeleteBillMessageDialog(context, false, i, callBack).show();
                return false;
            }
        });
        return view;
    }

    public void setData(List<TerminalBillMessage> messageList) {
        MyLogger.kLog().i("messageList=======================" + messageList.size());

        this.messageList.clear();
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        public LinearLayout mTimeLine;
        public TextView mTime;
        public TextView mContent;
        public TextView mDatetime_tv;
    }


}
